using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.Globalization;

using CNCSoft.EWinform;
using CNCSoft.EWinform.ControlUtils;
//using CNCSoft.EWinform.ERPApps;
using CNCSoft.EWinform.AGrid;
using CNCSoft.EData;

using Common;


namespace CNCSoft.ErpApps.TMS {

	/// <summary>
	/// Summary description for TienMatPhieuThuData.
	/// </summary>
	public class frmXuatVatTuData: TMSDataBase  {

        public enum TL
        {
            VATTU,
            VATTU_CHITIET
            
        }

        private string sqlVATTU =
            "SELECT [SoChungTu], [NgayChungTu], [MaDoiTac], [MaMucDichNhapXuat], [MaKhoVatTu], [DienGiai], [ChungTuLienQuan] " +
            "FROM [VATTU]";

        private string sqlVATTU_CHITIET =
            "SELECT [ID], [SoChungTu], [MaVatTu], [SoLuong],[DonGia], [ThanhTienNguyenTe], [TiGia], [ThanhTienVND], [Ghichu] " +
            "FROM [VATTU_CHITIET]";

        public frmXuatVatTuData()
        {

            ds.Tables.Add("VATTU");
            ds.Tables.Add("VATTU_CHITIET");
            //ds.Tables.Add("ListUser");

            //DanhMuc.fill(getTable(TL.ListUser), DanhMuc.TLDM.ListUser); 
          
			initData("('')", true);
		}

        public void initData(string strSoChungTu, bool firstLoading)
        {
			if (!firstLoading) {
                getTable(TL.VATTU_CHITIET).Clear();
                getTable(TL.VATTU).Clear();
			}
            SQLHelper.Fill(getTable(TL.VATTU), GlobalVariable.WSInfo.ConnectionString,
                sqlVATTU + " WHERE [SoChungTu] In " + strSoChungTu);

            SQLHelper.Fill(getTable(TL.VATTU_CHITIET), GlobalVariable.WSInfo.ConnectionString,
                sqlVATTU_CHITIET + " WHERE [SoChungTu] In " + strSoChungTu);

			if (firstLoading) {
				// Set primary key(s)
                getTable(TL.VATTU).PrimaryKey = new DataColumn[] { getTable(TL.VATTU).Columns["SoChungTu"] };
                getTable(TL.VATTU_CHITIET).PrimaryKey = new DataColumn[] { getTable(TL.VATTU_CHITIET).Columns["ID"] };

				// Set relations
                ds.Relations.Add("M_D", getTable(TL.VATTU).Columns["SoChungTu"],
                    getTable(TL.VATTU_CHITIET).Columns["SoChungTu"]);

                // Set default values
                getTable(TL.VATTU).Columns["SoChungTu"].DefaultValue = "";
                getTable(TL.VATTU).Columns["NgayChungTu"].DefaultValue = GlobalVariable.WSInfo.WorkingDate;
                //getTable(TL.VATTU).Columns["MaMucDichNhap"].DefaultValue = "02";
                getTable(TL.VATTU).Columns["DienGiai"].DefaultValue = "";
                getTable(TL.VATTU).Columns["ChungTuLienQuan"].DefaultValue = "";

                //Detail
                getTable(TL.VATTU_CHITIET).Columns["ID"].AutoIncrement = true;
                getTable(TL.VATTU_CHITIET).Columns["ID"].AutoIncrementStep = 1;
                getTable(TL.VATTU_CHITIET).Columns["SoLuong"].DefaultValue = 0;
                getTable(TL.VATTU_CHITIET).Columns["DonGia"].DefaultValue = 0;
                getTable(TL.VATTU_CHITIET).Columns["ThanhTienNguyenTe"].DefaultValue = 0;
                getTable(TL.VATTU_CHITIET).Columns["TiGia"].DefaultValue = 1;
                getTable(TL.VATTU_CHITIET).Columns["ThanhTienVND"].DefaultValue = 0;
                getTable(TL.VATTU_CHITIET).Columns["GhiChu"].DefaultValue = "";
			}
           
		}

		public bool createNewRow() {
			try {
                initData("('')", false);

                DataRow dr = getTable(TL.VATTU).NewRow();
                vg.DateForGenerating = GlobalVariable.WSInfo.WorkingDate;   //Generate theo ngay dang nhap
                vg.Prefix = "PX";
                vg.EndingFormat = "0000";
                vg.VoucherNoFormatDesign = VoucherNoGenerator.VoucherNoFormat.Prefix_YY_MM_Delimiter_SerialNo;
                dr["SoChungTu"] = vg.generateVoucherNo("VATTU", "SoChungTu");
                getTable(TL.VATTU).Rows.Add(dr);

				return true;
			}
			catch {
				MessageBox.Show(RS.ERROR_CREATE_NEW_ROW, RS.ERROR_CREATE_NEW_ROW_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public bool update() {
			try {
                new SimpleMasterDetailUpdater(sqlVATTU, sqlVATTU_CHITIET, getTable(TL.VATTU), getTable(TL.VATTU_CHITIET), GlobalVariable.WSInfo.ConnectionString);
                ds.AcceptChanges();
				return true;
			} catch (Exception ex) {
				MessageBox.Show(RS.ERROR_UPDATE +"\n" +ex.Message, RS.ERROR_UPDATE_CAPTION, MessageBoxButtons.OK, MessageBoxIcon.Error);
			}
			return false;
		}

		public DataTable getTable(TL index) {
			return ds.Tables[(int)index];
		}

	}
}
